package com.js.nickcar.module.addcar.series;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

import com.js.nickcar.base.BaseRecyclerViewFragment;
import com.js.nickcar.entity.remote.CarBrandBean;
import com.js.nickcar.entity.remote.CarModelBean;
import com.js.nickcar.entity.remote.CarSeriesBean;
import com.js.nickcar.module.addcar.baseinfo.BaseCarInfoFragment;
import com.js.nickcar.module.addcar.model.CarModelFragment;
import com.js.nickcar.widget.recycler.BaseRecyclerAdapter;

import static com.js.nickcar.module.addcar.baseinfo.BaseCarInfoFragment.REQUEST_CODE_CAR_MODEL;

/**
 * Created by LZY on 2017/9/30.
 */

public class CarBrandSeriesFrangment extends BaseRecyclerViewFragment<CarSeriesBean, CarBrandDetailPresenter> implements CarBrandDetailView {

    public static final String EXTRA_CAR_BRAND = "car_brand";



    @Override
    protected BaseRecyclerAdapter<CarSeriesBean> getRecyclerAdapter() {
        return new CarBrandSeriesAdapter(getContext());
    }

    @Override
    protected CarBrandDetailPresenter createPresenter() {
        return new CarBrandDetailPresenter();
    }

    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);
        CarBrandBean carBrand = bundle.getParcelable(EXTRA_CAR_BRAND);
        mPresenter.setCarBrand(carBrand);
    }

    @Override
    public void onItemClick(int position, long itemId) {
        super.onItemClick(position, itemId);
        CarModelFragment modelFragment = CarModelFragment.getInstance(mAdapter.getItem(position));
        modelFragment.setTargetFragment(this, getTargetRequestCode());
        addFragment(modelFragment);
    }

    public static CarBrandSeriesFrangment getInstance(CarBrandBean item) {
        CarBrandSeriesFrangment detailFrangment = new CarBrandSeriesFrangment();
        Bundle args = new Bundle();
        args.putParcelable(EXTRA_CAR_BRAND, item);
        detailFrangment.setArguments(args);
        return detailFrangment;
    }

    @Override
    protected String getTitle() {
        CarBrandBean carBrand = mPresenter.getCarBrand();
        if (carBrand != null) {
            return carBrand.getBrand_name();
        }
        return "";
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && resultCode == Activity.RESULT_OK && requestCode == getTargetRequestCode()) {
            getTargetFragment().onActivityResult(requestCode,resultCode,data);
            finish();
        }
    }
}